# @file:   Makefile
# @author: M. P. Hayes, UCECE
# @date:   20 August 2007
# @brief:  Makefile for Temperature Controller

CC = avr-gcc
CFLAGS = -mmcu=atmega8 -Os -Wall -Wstrict-prototypes -W -g -std=c99

OBJCOPY = avr-objcopy
SIZE = avr-size

DEL = rm

# Default target.
all: tempcon.hex


# Compile: create object files from C source files.
tempcon.o: tempcon.c display.h pacer.h config.h
	$(CC) -c $(CFLAGS) tempcon.c -o tempcon.o

display.o: display.c display.h config.h
	$(CC) -c $(CFLAGS) display.c -o display.o

pacer.o: pacer.c pacer.h config.h
	$(CC) -c $(CFLAGS) pacer.c -o pacer.o

ui.o: ui.c ui.h config.h
	$(CC) -c $(CFLAGS) ui.c -o ui.o

button.o: button.c button.h config.h
	$(CC) -c $(CFLAGS) button.c -o button.o

control.o: control.c control.h config.h
	$(CC) -c $(CFLAGS) control.c -o control.o

system.o: system.c system.h config.h
	$(CC) -c $(CFLAGS) system.c -o system.o

heater.o: heater.c heater.h config.h
	$(CC) -c $(CFLAGS) heater.c -o heater.o
	
temp_sensor.o: temp_sensor.c temp_sensor.h config.h
	$(CC) -c $(CFLAGS) temp_sensor.c -o temp_sensor.o

ds18b20.o: ds18b20.c ds18b20.h config.h
	$(CC) -c $(CFLAGS) ds18b20.c -o ds18b20.o

u1wire.o: u1wire.c u1wire.h config.h
	$(CC) -c $(CFLAGS) u1wire.c -o u1wire.o



# Link: create ELF output file from object files.
tempcon.elf: tempcon.o display.o pacer.o ui.o button.o ui.o system.o temp_sensor.o heater.o ds18b20.o u1wire.o control.o
	$(CC) $(CFLAGS) $^ -o tempcon.elf
	$(SIZE) tempcon.elf


# Convert ELF to COFF for use in debugging / simulating in AVR Studio.
COFFCONVERT=$(OBJCOPY) --debugging \
	--change-section-address .data-0x800000 \
	--change-section-address .bss-0x800000 \
	--change-section-address .noinit-0x800000 \
	--change-section-address .eeprom-0x810000

tempcon.cof: tempcon.elf
	$(COFFCONVERT) -O coff-avr tempconf.elf tempcon.cof


# Create hex output file from ELF output file.
tempcon.hex: tempcon.elf
	$(OBJCOPY) -O ihex tempcon.elf tempcon.hex


# Target: documentation
.PHONY: docs
docs: 
	(cd doc; doxygen doxygen.config)


# Target: clean project.
.PHONY: clean
clean: 
	$(DEL) *.o *.elf *.hex

# Target: install project.
.PHONY: install
install: all
# Fuse low byte
# BODLEVEL = 1, BODEN = 1, SUT1 = 1, SUT0 = 0, 
# CKSEL3 = 0, CKSEL2 = 1, CKSEL1 = 0, CKSEL0 = 0 
# CKSEL 0001 => 1 MHz internal oscillator.
	sp12 -wpf tempcon.hex -wF11100001 -T1

# BODLEVEL = 1, BODEN = 1, SUT1 = 1, SUT0 = 0, 
# CKSEL3 = 0, CKSEL2 = 1, CKSEL1 = 0, CKSEL0 = 0 
# CKSEL 0100 => 8 MHz internal oscillator.
# Note if you want an 8 MHz clock, you also need to edit F_CPU in target.h
#	sp12 -wpf tempcon.hex -wF11100100 -T1

# my target to load the program
og: all
	sudo avrdude -p m8 -c enel206 -U flash:w:tempcon.hex
